package com.gmail.jaredjeya.awesomeplatformer.view;

import com.gmail.jaredjeya.awesomeplatformer.model.Game;
import com.gmail.jaredjeya.awesomeplatformer.model.UpdateThread;
import com.gmail.jaredjeya.awesomeplatformer.Constants;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class GameView extends SurfaceView implements SurfaceHolder.Callback{
	
	private Game game;
	private Display display;
	private UpdateThread updateThread;
	private RenderThread renderThread;
	private SurfaceHolder holder;
	

	public GameView(final Context context) {
		super(context);
		Log.v(Constants.DEBUG_TAG, "GameView initialised...");
		game = new Game(context);
		display = new Display(game);
		holder = getHolder();
		holder.addCallback(this);
		setFocusable(true);
		Handler handler = new Handler();
		updateThread = new UpdateThread(game, handler, context);
		renderThread = new RenderThread(holder, context, display, handler);
		
		this.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				game.getInputHandler().touch(event);
				return true;
			}
		});
		
		this.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				game.getInputHandler().key(keyCode, event);
				return false;
			}
		});
	}
	
	//THIS IS NEVER CALLED!!!!!!!!!!!!!!!!!!!!!!!!!!!
	public GameView(final Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.v(Constants.DEBUG_TAG, "GameView initialised...");
		game = new Game(context);
		display = new Display(game);
		holder = getHolder();
		holder.addCallback(this);
		setFocusable(true);
		Handler handler = new Handler();
		updateThread = new UpdateThread(game, handler, context);
		renderThread = new RenderThread(holder, context, display, handler);
		
		this.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				game.getInputHandler().touch(event);
				return true;
			}
		});
		
		this.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				game.getInputHandler().key(keyCode, event);
				return false;
			}
		});
	}
	//THIS IS NEVER CALLED!!!!!!!!!!!!!!!!!!!!!!!!!!!

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		//do nothing
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		updateThread.start();
		renderThread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		try {
			updateThread.join();
			renderThread.join();
		}
		catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
